package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IShopTypeService typeService;
    @Override
    public List<ShopType> queryTypeList() {
        //查询所有商铺类型
        //1、查询redis
        String key = "shop:type:list";
        String shopTypeList = stringRedisTemplate.opsForValue().get(key);
//        System.out.println(shopTypeList);
        //2、判断是否存在
        if(StrUtil.isNotBlank(shopTypeList)){
            //存在直接返回
            List<ShopType> list = JSONUtil.toList(shopTypeList,ShopType.class);
            System.out.println(list);
            return list;
        }
        //3、否，查询数据库
        List<ShopType> list = typeService.query().orderByAsc("sort").list();
        //4、判断sf
        if(list==null||list.size()==0){
            return null;
        }
        //保存redis
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(list));
        return list;
    }
}
